﻿/**************************************************************************
创建日期:	2019/7/14 21:36:36     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)    ZhCun.cn

描	述：
记	录：
***************************************************************************/
using System;
using ZhCun.DbCore.Cores;
using ZhCun.DBModel;
using ZhCun.Utils;
using ZhCun.WebUtils.Authorizes;

namespace ZhCun.WebUtils.DB
{
    #region View Models

    public class UserLoginArg
    {
        public string LoginName { set; get; }

        public string LoginPwd { set; get; }

        public string VerifyCode { get; set; }

        public string LoginSign { set; get; }
    }

    public class UserLoginRet
    {
        public string Token { get; set; }
        public string UserName { get; set; }
        public DateTime ExpiresTime { get; set; }
    }

    public class User_ChangePwd
    {
        public string OldPwd { set; get; }

        public string NewPwd1 { set; get; }

        public string NewPwd2 { set; get; }
    }

    #endregion

    public class UserBaseBLL : BLLBase
    {
        protected virtual void SetLoginVerify(QueryCondition<Sys_User> query, UserLoginArg arg, int loginType)
        {
            query.WhereAnd(s => s.LoginName == arg.LoginName && s.LoginPwd == arg.LoginPwd);
        }

        protected LoginUser ToLoginUser(Sys_User user)
        {
            LoginUser loginUser = new LoginUser
            {
                UserId = user.Id,
                RoleId = user.RoleId,
                UserName = user.UserName,
            };
            return loginUser;
        }

        /// <summary>
        /// 登陆验证获取当前登陆用户
        /// </summary>
        /// <param name="arg">登陆参数</param>
        /// <param name="loginType">登陆类型 0：page，1：webapi</param>
        /// <returns></returns>
        public virtual ApiResult<LoginUser> Login(UserLoginArg arg, int loginType)
        {
            var query = DB.CreateQuery<Sys_User>();
            SetLoginVerify(query, arg, loginType);
            var user = DB.Query<Sys_User>(query).ToEntity();
            if (user == null)
            {
                return this.RetErr<LoginUser>("用户名或密码错误");
            }
            LoginUser loginUser = ToLoginUser(user);

            return this.RetOK(loginUser);
        }

        public ApiResult SaveInfo(Sys_User m)
        {
            m.ClearChangedState();
            m.SetFieldChanged(Sys_User.CNUserName, Sys_User.CNMobileNumber);
            var ret = DB.Update(m, s => s.Id == CurrUserId);
            if (ret.RowCount == 1) return this.RetOK("保存成功");
            return this.RetErr("保存失败");
        }

        public ApiResult ChangePwd(User_ChangePwd arg)
        {
            if (arg.NewPwd1 != arg.NewPwd2)
            {
                return this.RetErr("两次密码不一致");
            }
            Sys_User user = new Sys_User();
            var ret = DB.Update(user, s => s.Id == CurrUserId && s.LoginPwd == arg.OldPwd);

            if (ret.RowCount == 1) return this.RetOK("修改密码成功");
            return this.RetErr("修改密码失败,原密码错误");
        }

        public ApiResult LoginOut()
        {
            return this.RetOK("退出成功");
        }

        public Sys_User GetUserInfo()
        {
            var user = DB.Query<Sys_User>(s => s.Id == CurrUserId).ToEntity();
            return user;
        }
    }
}